<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/14
 * Time: 15:50
 */
require('env.php');
require('init.inc.php');
include_once 'functions.php';
if (isset($_GET['OrderProdDBId'])) {
    $OrderProdDBId = $_GET['OrderProdDBId'];
} else {
    $OrderProdDBId = '';
}

$tongji1 = home_tongji(1);
$tongji2 = home_tongji(2);
$tongji3 = home_tongji(3);
$index_std = ShopConfig::getConfig();
?>
<!DOCTYPE html>
<html>
<style>
    .modal-cart {
        width: 100%;
        text-align: center;
    }

    .modal-btn {
        width: 45%;
        text-align: center;
        height: 40px;
        background-color: #a94442;
        color: white;
    }

    .modal-title_clear {
        margin-left: 90%;
        font-family: "Arial Black";
        font-size: 15px;
        display: block;
        width: 50px;
        height: 25px;
        border: 1px solid #CCC;
        background-color: #dff0d8;
        color: black;
    }
</style>
<?php include("header.php"); ?>

<body>
<div class="modal-cart">
    <div class="modal-header">
        <h4 class="modal-title_n">我已經選購的商品</h4>
        <a class="modal-title_clear" onclick="clearCart()">清空</a>
    </div>

    <div class="area-loading" id="shopping-cart" style="display: block;">
        <table class="table table-cart" data-allow-addtional-products="true">
            <tbody>
            <tr class="success">
                <th width="50"></th>
                <th>品項</th>
                <th class="right" width="100">單價</th>
                <th class="right" width="100">數量</th>
                <th class="right" width="100">總計</th>
            </tr>
            <?php
            $order_products = OrderProduct::getOrdersProds($_COOKIE['ansyuserid']);
            $totalPrice = 0;
            if ($order_products) {
                foreach ($order_products as $ord) {
                    $totalPrice += ($ord['products_special_price'] * $ord['product_nums']);
                }

                foreach ($order_products as $order) {
                    ?>
                    <tr>
                        <td class="center">
                            <a class="btn btn-default" data-remote="true"
                               onclick="delord('<?php echo $order['oid'] ?>')"><i
                                    class="fa fa-times"></i></a>
                        </td>
                        <td>
                            <?php echo $order['products_name'] ?>
                        </td>
                        <td class="right">
                            <?php echo $order['products_special_price'] ?>
                        </td>
                        <td class="right">
                            <?php echo $order['product_nums'] ?>
                        </td>
                        <td class="right"> <?php echo $order['products_special_price'] * $order['product_nums'] ?></td>
                    </tr>
                <?php } ?>
                <tr>
                    <td class="right" colspan="4">
                        <strong>運費</strong>
                    </td>
                    <td class="right">
                        <strong class="font-danger">
                            免運
                        </strong>
                    </td>
                </tr>
                <tr>
                    <td class="right" colspan="4">
                        <strong>總計</strong>
                    </td>
                    <td class="right">
                        <strong> <?php echo $totalPrice ?> </strong>
                    </td>
                </tr>
            <?php } else {
                ?>
                購物車目前是空的
            <?php } ?>
            </tbody>
        </table>
        <div id="form-coupon"></div>
    </div>

    <div>
        <input class="modal-btn" type="button" value="立即結賬" onclick="jiezhang()"/>
    </div>
    <div>
        <input class="modal-btn" type="button" value="回到商城首页继续购买" onclick="goHome()"/>
    </div>
</div>
<div style="display:none;">
    <?php echo str_replace("&nbsp;",' ',htmlspecialchars_decode($tongji1['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;",' ',htmlspecialchars_decode($tongji2['tongji'], ENT_QUOTES)) . '<br/>' . str_replace("&nbsp;",' ',htmlspecialchars_decode($tongji3['tongji'], ENT_QUOTES)) . '<br/>'; ?>
</div>
<script type="text/javascript">
    //首页订单页面可以删除
    function delord(oid) {
        if (!confirm("确定要刪除嗎？")) {
            return;
        }
        //数据库id
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                order_db_id: oid,
                isUpdate: 1
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            },
            error: function () {
                alert("删除失败！");
            }
        });
    }


    //清空购物车
    function clearCart(oid) {
        if (!confirm("确定要清空嗎？")) {
            return;
        }
        //数据库id
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                isUpdate: 'all'
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.reload();
                } else {
                    alert("清空失败");
                }
            },
            error: function () {
                alert("清空失败！");
            }
        });
    }

    function jiezhang() {
        $.ajax({
            url: 'order.php',// 跳转到 action
            data: {
                isUpdate: -1
            },
            type: 'post',
            cache: false,
            dataType: 'json',
            success: function (data) {
                if (data.msg == "true") {
                    window.location.href = '<?php echo SERVER_ROOT?>' + 'invoicing_stp1.php?OrderProdDBId=' + '<?php echo $OrderProdDBId?>';
                } else {
                    alert("當前購物車為空，請先回首页選購商品");
                    window.location.href = '<?php echo SERVER_ROOT?>' + 'index.php';
                }
            },
            error: function () {
                alert("服務器錯誤！");
                return;
            }
        });
    }

    function goHome() {
        window.location.href = '<?php echo SERVER_ROOT?>' + 'index.php';
//        $.ajax({
//            url: 'order.php',// 跳转到 action
//            data: {
//                isUpdate: -1
//            },
//            type: 'post',
//            cache: false,
//            dataType: 'json',
//            success: function (data) {
//                if (data.msg == "true") {
//                    window.location.href = '<?php //echo SERVER_ROOT?>//' + 'index.php';
//                } else {
//                    alert("當前購物車為空，請先回首页選購商品");
//                    window.location.href = '<?php //echo SERVER_ROOT?>//' + 'index.php';
//                }
//            },
//            error: function () {
//                alert("服務器錯誤！");
//                return;
//            }
//        });
    }
</script>
</body>
</html>